﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Xml.Linq;

using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;
using System.Globalization;
using System.Threading;

namespace ThousandFunClient
{
    public partial class Fm_TF_Config : Form
    {
        public Fm_TF_Config()
        {
            InitializeComponent();
        }


        private void btn_save_Click(object sender, EventArgs e)
        {

            try
            {
                XElement xe = XElement.Load(TFConfig.ConfigXMLAddress);
                XElement portAName = xe.Element("PortA").Element("PortName");
                portAName.Value = portA.PortName;

                XElement portBName = xe.Element("PortB").Element("PortName");
                portBName.Value = portB.PortName;

                XElement portCName = xe.Element("PortC").Element("PortName");
                portCName.Value = portC.PortName;

                xe.Save(TFConfig.ConfigXMLAddress);

                TFConfig.GetInstance().init();

                MessageBox.Show("配置保存成功，请重新启动程序。");
                Application.Exit();

            }
            catch(Exception ex)
            {
                Constants.syslogger.Error("配置保存出错",ex);


            }

        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Fm_TF_Config_Load(object sender, EventArgs e)
        {
            List<string> useports = new List<string>();

            string portaname = TFConfig.GetInstance().PortConfigA.PortName;
            string portbname = TFConfig.GetInstance().PortConfigB.PortName;
            string portcname = TFConfig.GetInstance().PortConfigC.PortName;

            for (int i = 1; i < 50; i++)
            {

                try
                {
                    SP_main.PortName = "com" + i.ToString();
                    SP_main.Open();
                    SP_main.Close();
                }
                catch (Exception ec)
                {
                    
                    continue;
                }


                String comcc = "com" + i.ToString();
                useports.Add(comcc);
            }

            if (portaname.Equals(Constants.NullStr))
            {
                rbtn_gb_nvl.Select();
            }
            if (portbname.Equals(Constants.NullStr))
            {
                rbtn_gbportB_nvl.Select();
            }

            if (portcname.Equals(Constants.NullStr))
            {
                rbtn_gbportC_nvl.Select();
            }

            if (useports.Count > 0)
            {
                rbtn_gb_1.Text = useports[0];
                rbtn_gbportB_1.Text = useports[0];
                rbtn_gbportC_1.Text = useports[0];

                if(portaname.Equals(useports[0]))
                {
                    rbtn_gb_1.Select();
                }
                if (portbname.Equals(useports[0]))
                {
                    rbtn_gbportB_1.Select();
                }
                if (portcname.Equals(useports[0]))
                {
                    rbtn_gbportC_1.Select();
                }
            }
            else
            {
                rbtn_gb_1.Visible = false;
                rbtn_gbportB_1.Visible = false;
                rbtn_gbportC_1.Visible = false;
            }

            if (useports.Count > 1)
            {
                rbtn_gb_2.Text = useports[1];
                rbtn_gbportB_2.Text = useports[1];
                rbtn_gbportC_2.Text = useports[1];

                if (portaname.Equals(useports[1]))
                {
                    rbtn_gb_2.Select();
                }
                if (portbname.Equals(useports[1]))
                {
                    rbtn_gbportB_2.Select();
                }
                if (portcname.Equals(useports[1]))
                {
                    rbtn_gbportC_2.Select();
                }
            }
            else
            {
                rbtn_gb_2.Visible = false;
                rbtn_gbportB_2.Visible = false;
                rbtn_gbportC_2.Visible = false;

            }

            if (useports.Count > 2)
            {
                rbtn_gb_3.Text = useports[2];
                rbtn_gbportB_3.Text = useports[2];
                rbtn_gbportC_3.Text = useports[2];
                if (portaname.Equals(useports[2]))
                {
                    rbtn_gb_3.Select();
                }
                if (portbname.Equals(useports[2]))
                {
                    rbtn_gbportB_3.Select();
                }
                if (portcname.Equals(useports[2]))
                {
                    rbtn_gbportC_3.Select();
                }
            }
            else
            {
                rbtn_gb_3.Visible = false;
                rbtn_gbportB_3.Visible = false;
                rbtn_gbportC_3.Visible = false;
            }

            if (useports.Count > 3)
            {
                rbtn_gb_4.Text = useports[3];
                rbtn_gbportB_4.Text = useports[3];
                rbtn_gbportC_4.Text = useports[3];
                if (portaname.Equals(useports[3]))
                {
                    rbtn_gb_4.Select();
                }
                if (portbname.Equals(useports[3]))
                {
                    rbtn_gbportB_4.Select();
                }
                if (portcname.Equals(useports[3]))
                {
                    rbtn_gbportC_4.Select();
                }
            }
            else
            {
                rbtn_gb_4.Visible = false;
                rbtn_gbportB_4.Visible = false;
                rbtn_gbportC_4.Visible = false;
            }


            



        }

        private void rbtn_gbPortA_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton c = (RadioButton)sender;
            portA.PortName = c.Text;


        }

        private void rbtn_gbPortB_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton cg = (RadioButton)sender;
            portB.PortName = cg.Text;
        }

        private void rbtn_gbPortC_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton cgc = (RadioButton)sender;
            portC.PortName = cgc.Text;
        }

        private PortConfig portA = new PortConfig();
        private PortConfig portB = new PortConfig();
        private PortConfig portC = new PortConfig();

    }
}
